package com.example.choupaiapp.main.activity.publish;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.PreviewDataBean;
import com.example.choupaiapp.main.view.CustomVideoView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2020/2/17.
 */

public class PublishVideoPreviewActivity extends CommonActivity {

    private CustomVideoView videoView;
    private String videoPath;
    @BindView(R.id.play)
    ImageView play;
    @BindView(R.id.complete)
    TextView complete;
//    private int position;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_preview);

    }

    void playVideo() {
        if (videoView.isPlaying()) return;
        videoView.start();
    }

    @Override
    protected void initView() {
        complete.setText("删除");
        videoView = findViewById(R.id.video);
        videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                playVideo();
            }
        });

        Bundle bundle = getIntent().getExtras();
        videoPath = bundle.getString("path");
//        position = bundle.getInt("position");

        MediaController controller = new MediaController(this);
        controller.setAnchorView(videoView);
        controller.setMediaPlayer(videoView);
        videoView.setMediaController(controller);
        videoView.setVideoPath(videoPath);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                playVideo();
            }
        });

        videoView.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
            @Override
            public void onPlay() {
                play.setVisibility(View.GONE);
            }

            @Override
            public void onPause() {
                play.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initData() {

    }

    //    @Override
    @OnClick({R.id.complete, R.id.play})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.complete:
                ArrayList<String> list = new ArrayList<>();
                list.clear();
                PreviewDataBean cameraDataBean = new PreviewDataBean();
                cameraDataBean.setFlag("no_text");
                cameraDataBean.setIsVideo("yes");
                cameraDataBean.setList(list);
                EventBus.getDefault().post(cameraDataBean);
                finishActivity();
                break;
            case R.id.play:
                if (videoView.isPlaying()) {
                    videoView.resume();
                } else {
                    videoView.start();
                }
                break;
        }
    }
}
